package lex.lianliankan.utils;

import java.util.ArrayList;
import java.util.Formatter;
import java.util.Stack;

import org.lex.utils.Pair;

import lex.lianliankan.LianLianKanModel;

public class ModelUtils
{
	public static void dump( LianLianKanModel model )
	{
		Formatter f = new Formatter( System.out );
		for( int i = 0; i < model.getRowCount(); i++ )
		{
			for( int j = 0; j < model.getColumnCount(); j++ )
				f.format( "%2d ", model.getKey( i, j ) );
			f.format( "%n" );
		}
	}

	public static boolean canRemove( LianLianKanModel model, int row1, int col1, int row2, int col2 )
	{
		// ͬһ����Ԫ�����Ƴ�
		if( row1 == row2 || col1 == col2 )
			return false;
		// ��Ԫ��ֵ����ͬ�����Ƴ�
		if( model.getKey( row1, col1 ) != model.getKey( row2, col2 ) )
			return false;
		return false;
	}

	private static boolean findPath( LianLianKanModel model, int[][] visited, Stack<Cell> path,
		Cell to )
	{
		Cell from = path.peek();
		// ͬһ����Ԫ�����Ƴ�
		if( from.equals( to ) )
			return false;
		// ��Ԫ��ֵ����ͬ�����Ƴ�
		int fk = model.getKey( from.row, from.col );
		int tk = model.getKey( to.row, to.col );
		if( fk != tk )
			return false;
		// ������ֵͬ�����ڵ�Ԫ�����Ƴ�
		if( fk == tk )
		{
			if( (from.row == to.row && 1 == Math.abs( from.col - to.col ))
					|| (from.col == to.col && 1 == Math.abs( from.row - to.row )) )
			{
				path.push( to );
				return true;
			}
		}
		// ������Ȳ����Ƿ����·��
		if( from.row != -1 )
		{}
		return false;
	}

	private static Pair<Boolean, Cell[]> normalize( Stack<Cell> path )
	{
		ArrayList<Cell> tmp = new ArrayList<Cell>();
		tmp.add( path.pop() );
		while( false == path.isEmpty() )
		{}
		return null;
	}

	public static class Cell
	{
		public final int row;
		public final int col;

		public Cell( int row, int col )
		{
			this.row = row;
			this.col = col;
		}

		@Override
		public int hashCode()
		{
			final int prime = 31;
			int result = 1;
			result = prime * result + col;
			result = prime * result + row;
			return result;
		}

		@Override
		public boolean equals( Object o )
		{
			if( this == o )
				return true;
			if( o == null )
				return false;
			if( getClass() != o.getClass() )
				return false;
			Cell other = (Cell) o;
			if( col != other.col )
				return false;
			if( row != other.row )
				return false;
			return true;
		}

		@Override
		public String toString()
		{
			return "(" + row + "," + col + ")";
		}
	}
}
